﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using AssignmentsTableAdapters;

/// <summary>
/// Summary description for AssignmentsBLL
/// </summary>
[System.ComponentModel.DataObject]
public class AssignmentsBLL
{
    ILE_Global ileGlobal = new ILE_Global();

    private AssignmentsTableAdapter _assignmentsAdapter = null;
    protected AssignmentsTableAdapter AssignmentsAdapter
    {
        get
        {
            if (_assignmentsAdapter == null)
                _assignmentsAdapter = new AssignmentsTableAdapter();

            return _assignmentsAdapter;
        }
    }

    private QuizAssignmentsTableAdapter _quizAssignmentsAdapter = null;
    protected QuizAssignmentsTableAdapter QuizAssignmentsAdapter
    {
        get
        {
            if (_quizAssignmentsAdapter == null)
                _quizAssignmentsAdapter = new QuizAssignmentsTableAdapter();

            return _quizAssignmentsAdapter;
        }
    }

    private Assignments_QuizTableAdapter _assignmentsQuizAdapter = null;
    protected Assignments_QuizTableAdapter AssignmentsQuizAdapter
    {
        get
        {
            if (_assignmentsQuizAdapter == null)
                _assignmentsQuizAdapter = new Assignments_QuizTableAdapter();

            return _assignmentsQuizAdapter;
        }
    }

    private RelationsTableAdapter _relationsAdapter = null;
    protected RelationsTableAdapter RelationsAdapter
    {
        get
        {
            if (_relationsAdapter == null)
                _relationsAdapter = new RelationsTableAdapter();

            return _relationsAdapter;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, true)]
    public Assignments.AssignmentsDataTable GetAllAssignments()
    {
        return AssignmentsAdapter.GetAssignments();
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.AssignmentsDataTable GetAssignmentsByType(string Type)
    {
        return AssignmentsAdapter.GetAssignmentsByAssignmentType(Type);
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.QuizAssignmentsDataTable GetQuizAssignmentsByCourseIdAndStandardId(int CourseID, int StandardID)
    {
        Assignments.QuizAssignmentsDataTable returnTable = new Assignments.QuizAssignmentsDataTable();
        Assignments.RelationsDataTable relations = RelationsAdapter.GetRelationsByCourseIdAndStandardId(CourseID, StandardID);

        foreach (Assignments.RelationsRow relation in relations)
        {
            Assignments.QuizAssignmentsRow assignment = QuizAssignmentsAdapter.GetQuizAssignment(relation.AssignmentID)[0];
            if (assignment.AssignmentType == "Quiz")
            {
                returnTable.Rows.Add(assignment);
            }
        }

        return returnTable;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.AssignmentsRow GetAssignmentByAssignmentID(int AssignmentID)
    {
        return AssignmentsAdapter.GetAssignmentByAssignmentID(AssignmentID)[0];
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.Assignments_QuizRow GetAssignmentQuizByAssignmentID(int AssignmentID)
    {
        return AssignmentsQuizAdapter.GetQuizAssignment(AssignmentID)[0];
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Insert, false)]
    public int InsertAssignment(string AssignmentType, string AssignmentTitle, string Instruction,
        bool SendNotifyStart, bool SendNotifyEnd, int SendNotifyEndHours, bool SendResult,
        DateTime StartDateTime, DateTime EndDateTime)
    {
        return Convert.ToInt32(AssignmentsAdapter.InsertAssignment(AssignmentType, AssignmentTitle, Instruction, SendNotifyStart, SendNotifyEnd, SendNotifyEndHours, SendResult, DateTime.Now, StartDateTime, EndDateTime));
    }

    private bool InsertAssignmentQuiz(int AssignmentID, string QuizAssignment, int MaximumAttempts, int NumberOfQuestions)
    {
        return AssignmentsQuizAdapter.Insert(AssignmentID, QuizAssignment, MaximumAttempts, NumberOfQuestions) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Insert, false)]
    public int InsertQuizAssignment(string AssignmentTitle, string Instruction,
        string QuizAssignment, int MaximumAttempts, int NumberOfQuestions,
        bool SendNotifyStart, bool SendNotifyEnd, int SendNotifyEndHours, bool SendResult,
        DateTime StartDateTime, DateTime EndDateTime)
    {
        int AssignmentID = InsertAssignment("Quiz", AssignmentTitle, Instruction,
            SendNotifyStart, SendNotifyEnd, SendNotifyEndHours, SendResult,
            StartDateTime, EndDateTime);

        if (AssignmentID != 0)
        {
            bool result = InsertAssignmentQuiz(AssignmentID, QuizAssignment, MaximumAttempts, NumberOfQuestions);
            if (result)
                return AssignmentID;
        }

        return 0;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, false)]
    public bool UpdateAssignment(string AssignmentType, string AssignmentTitle, string Instruction,
        bool SendNotifyStart, bool SendNotifyEnd, int SendNotifyEndHours, bool SendResult,
        DateTime StartDateTime, DateTime EndDateTime, int AssignmentID)
    {
        return AssignmentsAdapter.Update(AssignmentType, AssignmentTitle, Instruction, SendNotifyStart, SendNotifyEnd, SendNotifyEndHours, SendResult, DateTime.Now, StartDateTime, EndDateTime, AssignmentID) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Delete, false)]
    public bool DeleteAssignment(int AssignmentID)
    {
        return AssignmentsAdapter.Delete(AssignmentID) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, false)]
    public bool UpdateAssignmentQuiz(string QuizAssignment, int MaximumAttempts, int NumberOfQuestions, int AssignmentID)
    {
        return AssignmentsQuizAdapter.Update(QuizAssignment, MaximumAttempts, NumberOfQuestions, AssignmentID) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.RelationsDataTable GetAllRelations()
    {
        return RelationsAdapter.GetRelations();
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.RelationsDataTable GetRelationsByAssignmentID(int AssignmentID)
    {
        return RelationsAdapter.GetRelationsByAssignmentID(AssignmentID);
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.RelationsDataTable GetRelationsByCourseIdAndStandardId(int CourseID, int StandardID)
    {
        return RelationsAdapter.GetRelationsByCourseIdAndStandardId(CourseID, StandardID);
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.RelationsDataTable GetRelationsByCourseIdAndSectionNumberAndStandardId(int CourseID, int SectionNumber, int StandardID)
    {
        return RelationsAdapter.GetRelationsByCourseIdAndSectionNumberAndStandardId(CourseID, SectionNumber, StandardID);
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.RelationsDataTable GetRelationsByCourseId(int CourseID)
    {
        return RelationsAdapter.GetRelationsByCourseID(CourseID);
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.RelationsDataTable GetRelationsByCourseIdAndSectionNumber(int CourseID, int SectionNumber)
    {
        return RelationsAdapter.GetRelationsByCourseIdAndSectionNumber(CourseID, SectionNumber);
    }

    //[System.ComponentModel.DataObjectMethodAttribute
    //    (System.ComponentModel.DataObjectMethodType.Select, false)]
    //public Assignments.AssignmentsDataTable GetAssignmentsByCourseIdAndSectionNumber(int CourseID, int SectionNumber)
    //{
    //    Assignments.AssignmentsDataTable results = new Assignments.AssignmentsDataTable();
    //    Assignments.RelationsDataTable relations = GetRelationsByCourseIdAndSectionNumber(CourseID, SectionNumber);

    //    foreach (Assignments.RelationsRow relation in relations)
    //    {
    //        Assignments.AssignmentsRow assignment = GetAssignmentByAssignmentID(relation.AssignmentID);
    //        results.Rows.Add(assignment);
    //    }

    //    return results;
    //}

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public bool InsertRelation(int AssignmentID, int CourseID, int SectionNumber, int StandardID)
    {
        return RelationsAdapter.Insert(AssignmentID, CourseID, SectionNumber, StandardID) == 1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public int IsRelationDuplicated(int CourseID, int SectionNumber, int StandardID)
    {
        return Convert.ToInt32(RelationsAdapter.IsDuplicated(CourseID, SectionNumber, StandardID));
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Delete, false)]
    public bool DeleteRelationsByAssignmentID(int AssignmentID)
    {
        return Convert.ToInt32(RelationsAdapter.DeleteRelationsByAssignmentID(AssignmentID)) != -1;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Assignments.AssignmentsDataTable GetAssignmentsByTypeAndStandardIdAndPastDue(int CourseID, int SectionNumber, int StandardID, string Type, string PastDue)
    {
        Assignments.AssignmentsDataTable results = new Assignments.AssignmentsDataTable();

        Assignments.RelationsDataTable relations = new Assignments.RelationsDataTable();

        if (StandardID != 0)
        {
            relations = GetRelationsByCourseIdAndSectionNumberAndStandardId(CourseID, SectionNumber, StandardID);
        }
        else
        {
            relations = GetRelationsByCourseIdAndSectionNumber(CourseID, SectionNumber);
        }

        foreach (Assignments.RelationsRow relation in relations)
        {
            Assignments.AssignmentsRow assignment = GetAssignmentByAssignmentID(relation.AssignmentID);
            if (assignment.AssignmentType == Type)
            {
                switch (PastDue)
                {
                    case "Before":
                        if (assignment.EndDateTime >= DateTime.Now)
                            results.ImportRow(assignment);
                        break;
                    case "Past":
                        if (assignment.EndDateTime < DateTime.Now)
                            results.ImportRow(assignment);
                        break;
                    default:
                        results.ImportRow(assignment);
                        break;
                }
            }
        }

        return results;
    }
}
